﻿using JT808.DotNetty.Core.Codecs;
using System.Collections.Concurrent;

namespace JTActiveSafety.DotNetty
{
    public class JTActiveSafetyAtomicCounterServiceFactory
    {
        private readonly ConcurrentDictionary<JTActiveSafetyTransportProtocolType, JTActiveSafetyAtomicCounterService> cache;

        public JTActiveSafetyAtomicCounterServiceFactory()
        {
            cache = new ConcurrentDictionary<JTActiveSafetyTransportProtocolType, JTActiveSafetyAtomicCounterService>();
        }

        public JTActiveSafetyAtomicCounterService Create(JTActiveSafetyTransportProtocolType type)
        {
            if(cache.TryGetValue(type,out var service))
            {
                return service;
            }
            else
            {
                var serviceNew = new JTActiveSafetyAtomicCounterService();
                cache.TryAdd(type, serviceNew);
                return serviceNew;
            }
        }
    }
}
